package com.gongpingjialoan.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.category.CategoryActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.adapter.HotAdapter;
import com.gongpingjialoan.bean.HotCarBean;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.data.CarListFilterData;
import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.utility.AbStrUtil;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/17.
 */
public class HotParamersView extends FrameLayout implements View.OnClickListener {

    private MyGridView mMyGridView;

    private List<HotCarBean> mHotCarBeans;

    private CategoryData mCategoryData;

    private HotAdapter mHotAdapter;

    private TextView numTextView;

    private View distanceView, priceView, dealerView, ksView;

    private boolean isLoadBrand = false;


    public HotParamersView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(getContext()).inflate(R.layout.hot_paramer_layout, this, true);
        mMyGridView = (MyGridView) findViewById(R.id.gridView);
        distanceView = findViewById(R.id.distance);
        priceView = findViewById(R.id.jiangjia);
        ksView = findViewById(R.id.ks_layout);
        numTextView = (TextView) findViewById(R.id.nums);
        dealerView = findViewById(R.id.renzheng);
        distanceView.setOnClickListener(this);
        priceView.setOnClickListener(this);
        dealerView.setOnClickListener(this);
        ksView.setOnClickListener(this);
        mMyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HotCarBean hotCarBean = mHotCarBeans.get(position);
                if ("price".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setPrice(hotCarBean.getPrice());
                    CarListFilterData.getInstance().setShow_price(hotCarBean.getName());
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                } else if ("car_model".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setVehicle_model(hotCarBean.getCar_model());
                    CarListFilterData.getInstance().setShow_vehicle_model(hotCarBean.getName());
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                } else if ("brand".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setBrandName(hotCarBean.getName());
                    CarListFilterData.getInstance().setBrandSlug(hotCarBean.getBrand());
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                } else if ("all_brand".equals(hotCarBean.getType())) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), CategoryActivity.class);
                    intent.putExtra("needModelDetailFragment", false);
                    intent.putExtra("isFromFilter", true);
                    MainActivity.main.startActivityForResult(intent, MainActivity.main.ALL_BRAND);
                } else if ("price_other".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setPrice(hotCarBean.getPrice());
                    CarListFilterData.getInstance().setShow_price("5万以内");
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                } else if ("index".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setSort(hotCarBean.getIndex());
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                } else if ("age_mile".equals(hotCarBean.getType())) {
                    CarListFilterData.getInstance().clear();
                    CarListFilterData.getInstance().setYear("201６-201７");
                    CarListFilterData.getInstance().setShow_age("1年以内");
                    CarListFilterData.getInstance().setMile("0-1.5");
                    CarListFilterData.getInstance().setShowmile("0-1.5万公里");
                    CarListFilterData.getInstance().setIsCleanData(true);
                    MainActivity.main.gotocheshi();
                }

            }
        });
        initData();
        if (GPJApplication.getInstance().mAppData != null && "success".equals(GPJApplication.getInstance().mAppData.getStatus())) {
            try {
                setNums(GPJApplication.getInstance().mAppData.getData().getCar_source_count().getValue());
            } catch (Exception e) {
            }
        }
    }

    private void initData() {
        mHotCarBeans = new ArrayList<>();
        HotCarBean hotCarBean;


        hotCarBean = new HotCarBean();
        hotCarBean.setType("price");
        hotCarBean.setName("5-10万");
        hotCarBean.setPrice("5-10");
        mHotCarBeans.add(hotCarBean);

        hotCarBean = new HotCarBean();
        hotCarBean.setType("price");
        hotCarBean.setName("10-15万");
        hotCarBean.setPrice("10-15");
        mHotCarBeans.add(hotCarBean);

        hotCarBean = new HotCarBean();
        hotCarBean.setType("price");
        hotCarBean.setName("15-20万");
        hotCarBean.setPrice("15-20");
        mHotCarBeans.add(hotCarBean);

        hotCarBean = new HotCarBean();
        hotCarBean.setType("price");
        hotCarBean.setName("20万以上");
        hotCarBean.setPrice("20-");
        mHotCarBeans.add(hotCarBean);

        hotCarBean = new HotCarBean();
        hotCarBean.setType("car_model");
        hotCarBean.setName("SUV");
        hotCarBean.setCar_model("suv");
        mHotCarBeans.add(hotCarBean);


        hotCarBean = new HotCarBean();
        hotCarBean.setType("price_other");
        hotCarBean.setName("练手车");
        hotCarBean.setPrice("0-5");
        mHotCarBeans.add(hotCarBean);

        hotCarBean = new HotCarBean();
        hotCarBean.setType("age_mile");
        hotCarBean.setName("准新车");
        hotCarBean.setMile("0-1.5");
        hotCarBean.setAge("2016-2017");
        mHotCarBeans.add(hotCarBean);


        hotCarBean = new HotCarBean();
        hotCarBean.setType("index");
        hotCarBean.setName("超值");
        hotCarBean.setIndex("-gpj_index");
        mHotCarBeans.add(hotCarBean);

        mHotAdapter = new HotAdapter(mHotCarBeans, getContext());
        mMyGridView.setAdapter(mHotAdapter);

        mCategoryData = GPJApplication.getInstance().getCategoryData();
        if (mCategoryData.mHotBrandList.size() > 0) {
            initBrand();
        }

    }

    public void initBrand() {
        if (isLoadBrand) return;
        HotCarBean hotCarBean;
        List<Map<String, String>> hots = mCategoryData.mHotBrandList;
        int size = hots.size();
        for (int i = 0; i < size; i++) {
            if (i >= 3) break;
            hotCarBean = new HotCarBean();
            hotCarBean.setType("brand");
            hotCarBean.setName(hots.get(i).get("name"));
            hotCarBean.setBrand(hots.get(i).get("slug"));
            hotCarBean.setIsShowImg(true);
            hotCarBean.setUrl(Const.IMG_DOMAIN + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                    + hots.get(i).get("logo_img"));
            mHotCarBeans.add(hotCarBean);
        }

        hotCarBean = new HotCarBean();
        hotCarBean.setType("all_brand");
        hotCarBean.setName("全部品牌>");
        hotCarBean.setBrand("");
        mHotCarBeans.add(hotCarBean);
        isLoadBrand = true;
        mHotAdapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (v == distanceView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_nearbyCar", System.currentTimeMillis(), 1f));
            CarListFilterData.getInstance().clear();
            CarListFilterData.getInstance().setSort("distance");
            CarListFilterData.getInstance().setIsCleanData(true);
            MainActivity.main.gotocheshi();
        } else if (v == priceView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_priceCutCar", System.currentTimeMillis(), 1f));
            CarListFilterData.getInstance().clear();
            CarListFilterData.getInstance().setIsCut("1");
            CarListFilterData.getInstance().setIsCleanData(true);
            MainActivity.main.gotocheshi();
        } else if (v == dealerView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_certifiedCar", System.currentTimeMillis(), 1f));
            CarListFilterData.getInstance().clear();
            CarListFilterData.getInstance().setIs_certify("1");
            CarListFilterData.getInstance().setIsCleanData(true);
            MainActivity.main.gotocheshi();
        } else if (v == ksView) {
            StepMonitor.getInstance().addMonitor(new Monitor("click", "eval_fastFindingCar", System.currentTimeMillis(), 1f));
            CarListFilterData.getInstance().clear();
            CarListFilterData.getInstance().setIsCleanData(true);
            MainActivity.main.gotocheshi();
        }
    }

    public void setNums(int num) {
        if (num > 0) {
            ksView.setVisibility(View.VISIBLE);
            if (num < 1000) {
                numTextView.setText(num + "");
            } else {
                numTextView.setText(AbStrUtil.getNumString(num));
            }
        }
    }

}
